package com.example.hotel_lxy.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.hotel_lxy.R;
import com.example.hotel_lxy.application.ARouterPath;
import com.example.hotel_lxy.bean.BeanBean;
import com.example.hotel_lxy.bean.ContainerBean;
import com.example.hotel_lxy.bean.EventBusBean;
import com.example.hotel_lxy.bean.EventBusResereBean;
import com.example.hotel_lxy.bean.MealLabelDishBean;
import com.example.hotel_lxy.bean.MealLabelDishTypeBean;
import com.example.hotel_lxy.bean.PersionInfo;
import com.example.hotel_lxy.bean.StandardCbBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.ui.dialog.SelectedFoodDialog;
import com.example.hotel_lxy.utils.SPUtils;
import com.ysxsoft.common_base.base.BaseActivity;
import com.ysxsoft.common_base.base.RBaseAdapter;
import com.ysxsoft.common_base.base.RViewHolder;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.ysxsoft.common_base.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 选择餐标
 */
@Route(path = "/main/MealLabelActivity")
public class MealLabelActivity extends BaseActivity implements View.OnClickListener {


    List<PersionInfo> listinfoInfos;
    @BindView(R.id.listview)
    RecyclerView listview;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.fragment_container)
    RecyclerView fragmentContainer;
    @BindView(R.id.edit_search)
    EditText editSearch;
    @BindView(R.id.text_determine)
    TextView textDetermine;
    @BindView(R.id.text_number)
    TextView textNumber;
    @BindView(R.id.text_empty)
    TextView textEmpty;
    @BindView(R.id.fra_dialog)
    FrameLayout fraDialog;

    private String ids;
    private String did;//餐标id
    private int position_number = 0;//用于区分是否是套餐
    private int position_number1 = 0;//用于区分是否是套餐
    RBaseAdapter<PersionInfo> rBaseAdapter;//适配器
    private List<ContainerBean> mlist = new ArrayList<>();//数据源
    private List<ContainerBean> mlistCan = new ArrayList<>();//餐标数据源
    private List<ContainerBean> mlistData = new ArrayList<>();//加减餐标数据源
    List<TwoBean> twolist = new ArrayList<>();
    @Autowired
    String cb_did;
    @Autowired
    String cb_id;
    @Autowired
    String tid;
    int size;
    public static void start(String cb_did,String cb_id,String tid) {
        ARouter.getInstance().build(ARouterPath.getMealLabelActivity())
                .withString("cb_did",cb_did)
                .withString("cb_id",cb_id)
                .withString("tid",tid)
                .navigation();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_meal_label;
    }

    @Override
    public void doWork() {
        super.doWork();
        ARouter.getInstance().inject(this);
        back.setOnClickListener(this);
        textDetermine.setOnClickListener(this);
        textEmpty.setOnClickListener(this);
        fraDialog.setOnClickListener(this);
        editSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals("")) {
                    for (PersionInfo data : listinfoInfos) {
                        data.setChick(false);
                    }
                    position_number1=position_number;
                    position_number=2;
                    listinfoInfos.get(position_number1).setChick(false);
                    rBaseAdapter.notifyDataSetChanged();
                    showDataDetails("", s.toString());
                } else if (s.toString().equals("")) {
                    for (PersionInfo data : listinfoInfos) {
                        data.setChick(false);
                    }
                    position_number= position_number1;
                    listinfoInfos.get(position_number).setChick(true);
                    rBaseAdapter.notifyDataSetChanged();
                    if (position_number == 0) {
                        showDataStandard();
                    } else {
                        showDataDetails(ids, "");
                    }

                }
            }
        });
        //菜品分类
        showDataTitle();
//        //获取菜品
        showDataStandardTwo();
    }
    //获取餐标
    private void showDataStandardTwo() {
        OkHttpUtils.post()
                .url(Api.POST_SCHEDULED_STANDARD)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .addParams("bid", "" + SPUtils.get(MealLabelActivity.this, "sched_id", "").toString())
                .addParams("tid",tid)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "获取餐标onError2   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "获取餐标onResponse2   " + response);
                        BeanBean beanBean= JsonUtils.parseByGson(response, BeanBean.class);
                        if(beanBean.getCode().equals("4")){
                            SPUtils.put(mContext,"token","");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        StandardCbBean standardCbBean = JsonUtils.parseByGson(response, StandardCbBean.class);
                        if (beanBean.getCode().equals("0")) {
                            for (int i = 0; i < standardCbBean.getData().getData().size(); i++) {
                                StandardCbBean.DataBeanX.DataBean dataBean = standardCbBean.getData().getData().get(i);
                                if (dataBean != null) {
                                    ContainerBean container = new ContainerBean();
                                    container.setMoney("");
                                    container.setNumber("0");
                                    container.setName(dataBean.getName() + "");
                                    container.setId(dataBean.getId() + "");
                                    container.setDid(dataBean.getDid());
                                    mlistData.add(container);
                                }
                            }
                            if(!cb_id.equals("")){
                                for (int j = 0; j <mlistData.size() ; j++) {
                                    if(cb_id.contains(",")){
                                        String[] split = cb_id.split(",");
                                        for (int k = 0; k <split.length ; k++) {
                                            if(mlistData.get(j).getId().equals(split[k])){
                                                String number = mlistData.get(j).getNumber();
                                                if(number.equals("0")){
                                                    mlistData.get(j).setNumber(""+1);
                                                }else{
                                                    mlistData.get(j).setNumber(""+(Integer.valueOf(number)+1));
                                                }

                                            }
                                        }
                                    }else{
                                        if(mlistData.get(j).getId().equals(cb_id)){
                                            mlistData.get(j).setNumber("1");
                                        }
                                    }

                                }
                            }
                            new Handler().post(new Runnable() {
                                @Override
                                public void run() {
                                    showDataStandard();
                                }
                            });
                        }
                    }
                });
    }

    //餐标
    private void showDataStandard() {
        mlistCan.clear();
        OkHttpUtils.post()
                .url(Api.POST_SCHEDULED_STANDARD)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .addParams("bid", "" + SPUtils.get(MealLabelActivity.this, "sched_id", "").toString())
                .addParams("tid",tid)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "获取餐标onError1   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "获取餐标onResponse1   " + response);
                        BeanBean beanBean= JsonUtils.parseByGson(response, BeanBean.class);
                        if(beanBean.getCode().equals("4")){
                            SPUtils.put(mContext,"token","");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        StandardCbBean standardCbBean = JsonUtils.parseByGson(response, StandardCbBean.class);
                        if (beanBean.getCode().equals("0")) {
                            for (int i = 0; i < standardCbBean.getData().getData().size(); i++) {
                                StandardCbBean.DataBeanX.DataBean dataBean = standardCbBean.getData().getData().get(i);
                                if (dataBean != null) {
                                    ContainerBean container = new ContainerBean();
                                    container.setMoney(dataBean.getMoney());
                                    container.setNumber("0");
                                    container.setName(dataBean.getName() + "");
                                    container.setId(dataBean.getId() + "");
                                    container.setDid(dataBean.getDid());
                                    mlistCan.add(container);
                                }
                                for (int j = 0; j <mlistCan.size() ; j++) {
                                    String[] split = cb_id.split(",");
                                    for (int k = 0; k < split.length; k++) {
                                        if (mlistCan.get(j).getId().equals(split[k])) {
                                            String number = mlistCan.get(j).getNumber();
                                            if(number.equals("0")){
                                                mlistCan.get(j).setNumber(1 +"");
                                            }else{
                                                mlistCan.get(j).setNumber((Integer.valueOf(number)+1)+"");
                                            }

                                        }
                                    }
                                }

                                for (int j = 0; j < mlistData.size(); j++) {
                                    for (int k = 0; k < mlistCan.size(); k++) {
                                        if (mlistData.get(j).getId().equals(mlistCan.get(k).getId())) {
                                            mlistCan.get(k).setNumber(mlistData.get(j).getNumber());
                                        }
                                    }
                                }
                                new Handler().post(new Runnable() {
                                    @Override
                                    public void run() {
                                        showAdepter(mlistCan);
                                    }
                                });

                            }
                        }else{
                            new Handler().post(new Runnable() {
                                @Override
                                public void run() {
                                    showAdepter(mlistCan);
                                }
                            });
                        }
                    }
                });

    }

    //菜品分类
    private void showDataTitle() {
        listinfoInfos = new ArrayList<>();
        OkHttpUtils.post()
                .url(Api.POST_SCHEDULED_DISH_TYPE)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "菜品分类onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "菜品分类onResponse   " + response);
                        BeanBean beanBean= JsonUtils.parseByGson(response, BeanBean.class);
                        if(beanBean.getCode().equals("4")){
                            SPUtils.put(mContext,"token","");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                          mealLabelDishBean = JsonUtils.parseByGson(response, MealLabelDishTypeBean.class);
                        if (beanBean.getCode().equals("0")) {
                            size = mealLabelDishBean.getData().getDish_type().size();
                            PersionInfo info = new PersionInfo();
                            info.setNameString("自选套餐");
                            info.setId("" + SPUtils.get(MealLabelActivity.this, "sched_id", "").toString());
                            listinfoInfos.add( info);
                            for (int i = 0; i < mealLabelDishBean.getData().getDish_type().size(); i++) {
                                initinfo(i);
                            }
                            ids = SPUtils.get(MealLabelActivity.this, "sched_id", "").toString();
                            listinfoInfos.get(0).setChick(true);
                            listview.setLayoutManager(new LinearLayoutManager(MealLabelActivity.this));
                            rBaseAdapter = new RBaseAdapter<PersionInfo>(MealLabelActivity.this, R.layout.list_item, listinfoInfos) {
                                @Override
                                protected void fillItem(RViewHolder holder, PersionInfo item, int position) {
                                    TextView tv = holder.itemView.findViewById(R.id.tv);
                                    tv.setText("" + item.getNameString());
                                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            if (item.isChick()) {
                                                return;
                                            }
                                            cb_id="";
                                            position_number = position;
                                            position_number1=position;
                                            for (PersionInfo data : listinfoInfos) {
                                                data.setChick(false);
                                            }
                                            listinfoInfos.get(position).setChick(true);
                                            if(!editSearch.getText().toString().equals("")){
                                                editSearch.setText("");
                                            }

                                            if (position == 0) {
                                                showDataStandard();
                                            } else {
                                                ids = item.getId();
                                                showDataDetails(item.getId(), "");

                                            }
                                            notifyDataSetChanged();
                                        }
                                    });
                                    if (item.isChick()) {
                                        holder.itemView.setBackgroundColor(Color.parseColor("#ffffff"));
                                        tv.setTextColor(Color.parseColor("#696969"));

                                    } else {
                                        holder.itemView.setBackgroundColor(Color.parseColor("#F1F2F4"));
                                        tv.setTextColor(Color.parseColor("#919293"));
                                    }
                                }

                                @Override
                                protected int getViewType(PersionInfo item, int position) {
                                    return 0;
                                }
                            };
                            listview.setAdapter(rBaseAdapter);
                        }
                    }
                });
    }
    MealLabelDishTypeBean mealLabelDishBean;

    private void initinfo(int i) {
        MealLabelDishTypeBean.DataBean.DishTypeBean listBean = mealLabelDishBean.getData().getDish_type().get(i);
        showDataDetailsTwo(listBean.getId() + "", listBean.getName(),mealLabelDishBean.getData().getDish_type().size());
        PersionInfo info = new PersionInfo();
        info.setNameString(listBean.getName());
        info.setId(listBean.getId() + "");
        listinfoInfos.add(info);
    }

    int sizeq=0;
    //获取菜品
    private void showDataDetailsTwo(String s, String name, int sizes) {
        OkHttpUtils.post()
                .url(Api.POST_SCHEDULED_DISH)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .addParams("page", "1")
                .addParams("tid", "" + s)
                .addParams("name", "" + name)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "获取菜品onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "获取菜品onResponse   " + response);
                        BeanBean beanBean= JsonUtils.parseByGson(response, BeanBean.class);
                        if(beanBean.getCode().equals("4")){
                            SPUtils.put(mContext,"token","");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        MealLabelDishBean mealLabelDishBean = JsonUtils.parseByGson(response, MealLabelDishBean.class);
                        if (beanBean.getCode().equals("0")) {
                            for (int i = 0; i < mealLabelDishBean.getData().getList().size(); i++) {
                                MealLabelDishBean.DataBean.ListBean listBean = mealLabelDishBean.getData().getList().get(i);
                                TwoBean twoBean = new TwoBean();
                                twoBean.setId(listBean.getId() + "");
                                twoBean.setMoney(listBean.getMoney());
                                twoBean.setName(listBean.getName());
                                twoBean.setNumber("0");
                                twolist.add(twoBean);
                            }
                            Log.e("TAG",sizeq+"  "+sizes);
                            sizeq++;
                            if(sizeq==sizes){
                                String[] split = cb_did.split(",");
                                for (int q = 0; q < split.length; q++) {
                                    for (int ie = 0; ie < twolist.size(); ie++) {
                                        if (split[q].equals(twolist.get(ie).getId())) {
                                            int integer = Integer.valueOf(twolist.get(ie).getNumber()) + 1;
                                            twolist.get(ie).setNumber("" + integer);
                                        }
                                    }
                                }
                                String number = getNumber(twolist);
                                if (!number.equals("0")) {
                                    textNumber.setVisibility(View.VISIBLE);
                                    textNumber.setText("" +number);
                                } else {
                                    textNumber.setVisibility(View.GONE);
                                }
                            }
                        }
                    }
                });
    }


    //获取菜品
    private void showDataDetails(String id, String name) {
        mlist.clear();
        OkHttpUtils.post()
                .url(Api.POST_SCHEDULED_DISH)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .addParams("page", "1")
                .addParams("tid", "" + id)
                .addParams("name", "" + name)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "获取菜品onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "获取菜品onResponse   " + response);
                        BeanBean beanBean= JsonUtils.parseByGson(response, BeanBean.class);
                        if(beanBean.getCode().equals("4")){
                            SPUtils.put(mContext,"token","");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        MealLabelDishBean mealLabelDishBean = JsonUtils.parseByGson(response, MealLabelDishBean.class);
                        if (beanBean.getCode().equals("0")) {
                            for (int i = 0; i < mealLabelDishBean.getData().getList().size(); i++) {
                                MealLabelDishBean.DataBean.ListBean listBean = mealLabelDishBean.getData().getList().get(i);
                                ContainerBean container = new ContainerBean();
                                container.setMoney(listBean.getMoney());
                                container.setNumber("0");
                                container.setName(listBean.getName() + "");
                                container.setId(listBean.getId() + "");
                                mlist.add(container);
                            }

                            if (mlist.size() != 0) {
                                for (int i = 0; i < twolist.size(); i++) {
                                    for (int j = 0; j < mlist.size(); j++) {
                                        if (twolist.get(i).getId().equals(mlist.get(j).getId())) {
                                            mlist.get(j).setNumber(twolist.get(i).getNumber());
//                                            break;
                                        }
                                    }
                                }
                                showAdepter(mlist);
                            } else {
                                showAdepter(mlist);
                            }
                        }
                    }
                });
    }

    private void showAdepter(List<ContainerBean> mlist) {
        fragmentContainer.setLayoutManager(new LinearLayoutManager(this));
        RBaseAdapter<ContainerBean> rBaseAdapter = new RBaseAdapter<ContainerBean>(this, R.layout.item_meallable_rv, mlist) {
            @Override
            protected void fillItem(RViewHolder holder, ContainerBean item, int position) {
                TextView text_name = holder.itemView.findViewById(R.id.text_name);
                TextView text_money = holder.itemView.findViewById(R.id.text_money);
                ImageView img_reduce = holder.itemView.findViewById(R.id.img_reduce);
                TextView text_number = holder.itemView.findViewById(R.id.text_number);
                ImageView img_add = holder.itemView.findViewById(R.id.img_add);
                text_number.setText("" + item.getNumber());
                text_name.setText(item.getName());
                text_money.setText(item.getMoney());
                if (item.getNumber().equals("0")) {
                    img_reduce.setVisibility(View.INVISIBLE);
                    text_number.setVisibility(View.INVISIBLE);
                } else {
                    img_reduce.setVisibility(View.VISIBLE);
                    text_number.setVisibility(View.VISIBLE);
                }
                img_reduce.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int integer = Integer.valueOf(item.getNumber());
                        if (integer != 0) {
                            int j = integer - 1;
                            if (position_number == 0) {
                                did = item.getId();
                                mlistData.get(position).setNumber("" + j);
                                String[] split = item.getDid().split(",");
                                for (int i = 0; i < twolist.size(); i++) {
                                    for (int q = 0; q < split.length; q++) {
                                        if (split[q].equals(twolist.get(i).getId())) {
                                            int integer1 = Integer.valueOf(twolist.get(i).getNumber());
                                            if (integer1 == 0) {
                                                twolist.get(i).setNumber("0");
                                            } else {
                                                twolist.get(i).setNumber("" + j);
                                            }
                                        }
                                    }
                                }
                                mlistCan.get(position).setNumber(j + "");
                            } else {
                                for (int i = 0; i < twolist.size(); i++) {
                                    if (twolist.get(i).getId().equals(item.getId())) {
                                        twolist.get(i).setNumber("" + j);
                                    }
                                }
                                mlist.get(position).setNumber(j + "");
                            }
                        } else {
                            did = "";
                        }
                        Log.e("TAG","22");
                        String number = getNumber(twolist);
                        if (!number.equals("0")) {
                            textNumber.setVisibility(View.VISIBLE);
                            textNumber.setText("" + getNumber(twolist));
                        } else {
                            textNumber.setVisibility(View.GONE);
                        }
                        notifyDataSetChanged();

                    }
                });
                img_add.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int integer = Integer.valueOf(item.getNumber());
                        int j = integer + 1;
                        if (position_number == 0) {
                            did = item.getId();
                            mlistData.get(position).setNumber("" + j);
                            String[] split = item.getDid().split(",");
                            for (int i = 0; i < twolist.size(); i++) {
                                for (int q = 0; q < split.length; q++) {
                                    if (split[q].equals(twolist.get(i).getId())) {
                                        int t= Integer.valueOf(twolist.get(i).getNumber())+1;
                                        twolist.get(i).setNumber(""+t );
                                    }
                                }
                            }
                            mlistCan.get(position).setNumber(j + "");
                        } else {
                            for (int i = 0; i < twolist.size(); i++) {
                                if (twolist.get(i).getId().equals(item.getId())) {
                                    twolist.get(i).setNumber("" + j);
                                }
                            }
                            mlist.get(position).setNumber(j + "");
                        }
                        Log.e("TAG", "33 ");
                        String number = getNumber(twolist);
                        if (!number.equals("0")) {
                            textNumber.setVisibility(View.VISIBLE);
                            textNumber.setText("" + getNumber(twolist));
                        } else {
                            textNumber.setVisibility(View.GONE);
                        }
                        notifyDataSetChanged();

                    }
                });
            }

            @Override
            protected int getViewType(ContainerBean item, int position) {
                return 0;
            }
        };
        fragmentContainer.setAdapter(rBaseAdapter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.text_determine:
                if(twolist.size()!=0){
                    if(!getId(twolist).equals("")) {
                        String id = getId(twolist).substring(0, getId(twolist).length() - 1);
                        StringBuffer stringBuffer = new StringBuffer();

                        for (int i = 0; i < mlistData.size(); i++) {
                            if (!mlistData.get(i).getNumber().equals("0")) {
                                int integer = Integer.valueOf(mlistData.get(i).getNumber());
                                for (int j = 0; j < integer; j++) {
                                    stringBuffer.append(mlistData.get(i).getDid() + ",");
                                }
                            }
                        }
                        String id1 = getTcId(mlistCan);
                        if (!getId(twolist).equals("")) {
                            EventBusResereBean eventBusBean = new EventBusResereBean();
                            eventBusBean.setType(5);
                            eventBusBean.setDid(id);
                            if(!cb_id.equals("")){
                                eventBusBean.setId("");
                            }else{
                                if (id1.contains(",")) {
                                    String substring = id1.substring(0, id1.length() - 1);
                                    eventBusBean.setId(substring);
                                } else {
                                    eventBusBean.setId(id1);
                                }
                            }
                            EventBus.getDefault().post(eventBusBean); //普通事件发布
                        }
                        finish();
                    }else{
                        ToastUtils.shortToast(MealLabelActivity.this,"您还没点菜呢");
                    }
                }else{
                    ToastUtils.shortToast(MealLabelActivity.this,"请稍等..");
                }

                break;
            case R.id.text_empty:
                if (position_number == 0) {
                    showDataStandard();
                } else {
                    showDataDetails(ids, "");
                }
                getClean(twolist);
                getCanClean(mlistData);
                textNumber.setVisibility(View.GONE);
                textNumber.setText("");
                break;
            case R.id.fra_dialog:
                if(!getId(twolist).equals("")){
                    String ids=  getId(twolist).substring(0,getId(twolist).length()-1);
                    if (!getId(twolist).equals("")) {
                        SelectedFoodDialog.show(MealLabelActivity.this,null,ids);
                    }else{
                        ToastUtils.show(MealLabelActivity.this,"请先选择菜品");
                    }
                }
                break;
        }
    }

    //清空已选菜品
    private static String getClean(List<TwoBean> twolist) {
        int number = 0;
        for (int i = 0; i < twolist.size(); i++) {
            if (!twolist.get(i).getNumber().equals("0")) {
                twolist.get(i).setNumber("0");
            }
        }
        return "" + number;
    }

    //清空已选餐标
    private static String getCanClean(List<ContainerBean> twolist) {
        int number = 0;
        for (int i = 0; i < twolist.size(); i++) {
            if (!twolist.get(i).getNumber().equals("0")) {
                twolist.get(i).setNumber("0");
            }
        }
        return "" + number;
    }

    //获取已选菜品数量
    private static String getNumber(List<TwoBean> twolist) {
        int number = 0;
        for (int i = 0; i < twolist.size(); i++) {
            if(Integer.valueOf(twolist.get(i).getNumber())>1){
                number = number + 1;
            }else{
                number = number + Integer.valueOf(twolist.get(i).getNumber());
            }

        }
        return "" + number;
    }

    //获取已选菜品的id
    private static String getId(List<TwoBean> twolist) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < twolist.size(); i++) {
            if (!twolist.get(i).getNumber().equals("0")) {
                int integer = Integer.valueOf(twolist.get(i).getNumber());
                for (int j = 0; j < integer; j++) {
                    sb.append(twolist.get(i).getId() + ",");
                }
            }
        }
        return "" + sb.toString();
    }

    //获取已选菜品的id
    private static String getTcId(List<ContainerBean> mlistCan) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < mlistCan.size(); i++) {
            if (!mlistCan.get(i).getNumber().equals("0")) {
                int integer = Integer.valueOf(mlistCan.get(i).getNumber());
                for (int j = 0; j < integer; j++) {
                    sb.append(mlistCan.get(i).getId() + ",");
                }
            }
        }
        return "" + sb.toString();
    }


    public class TwoBean {
        private String name;
        private String id;
        private String money;
        private String number;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getMoney() {
            return money;
        }

        public void setMoney(String money) {
            this.money = money;
        }

        public String getNumber() {
            return number;
        }

        public void setNumber(String number) {
            this.number = number;
        }
    }
}